package com.qf.mybatis03.utils;

import cn.hutool.core.io.file.FileAppender;
import com.qf.mybatis03.entity.Book;

import java.io.File;
import java.util.List;

public class BookWriter {
    //该对象关联的文件路径
    private String filepath;

    public BookWriter(String filepath) {
        this.filepath = filepath;
    }

    /*
    生成的文本文件格式为csv（逗号分隔的文本文件）
    * */
    public void writeBooksToFile(List<Book> bookList) {
        FileAppender fileAppender = new FileAppender(new File(filepath), 16, true);
        for (Book book : bookList) {
            //book--->String
            String csv = book.toCSVString();
            fileAppender.append(csv);
        }
        fileAppender.flush();
    }
}
